<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>注册新用户</h1>
    <span>邮箱</span><input id="email" type="text"></br>
    <span>密码</span><input id="password" type="text"></br>
    <span>确认密码</span><input id="cfPassword" type="text"></br>
    <span>验证码</span><input id="code" type="text">&nbsp;<a onclick="getCode()" style="color: blue">获取验证码</a></br></br>
    <input type="button" value="注册" onclick="regist()"/></br>

<script type="text/javascript" src="../js/jquery-3.4.1.min.js"></script>
<script type="text/javascript">

    var lastCodeTime = 0;

    function getCode() {
        var curTime = new Date().getTime();
        var difTime = curTime-lastCodeTime;
        if(difTime/1000<60){
            alert("60秒内不可重复发送");
            return;
        }
        lastCodeTime = new Date().getTime();

        var email = $("#email").val();

        //发起请求
        var url = "http://127.0.0.1:9002/code/create/"+email;

        $.ajax({
            url: url,
            type: 'post',
            async:false,
            dataType : "text",
            data:{

            },
            timeout:5000,
            success:function (data) {
                if(data=="true"){
                    alert("验证码已发送！");
                }

            },
        })
    }

    function regist() {
        var email = $("#email").val();
        var password = $("#password").val();
        var cfPassword = $("#cfPassword").val();
        var code = $("#code").val();

        if(password!=cfPassword){
            alert("密码输入不一致！")
            return;
        }
        //发起请求
        var url = "http://127.0.0.1:9002/user/register/"+email+"/"+password+"/"+code;
        $.ajax({
            url: url,
            type: 'post',
            async:false,
            dataType : "text",
            data:{

            },
            timeout:5000,
            success:function (data) {
                if(data=="true"){
                    alert("注册成功！请去登录！");

                    var newUrl = "./login.html";
                    $(window).attr('location',newUrl);
                }else{
                    alert("验证码不正确或已过期！")
                }
            },
        })
    }








</script>

</body>
</html>